package com.winit.base;

import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.Timestamp;
import java.util.List;

/**
 * Created by wangjian on 17/7/3.
 */
public class BaseDelete {
    public static void deleteT(Connection conn, String sql, List<BaseParam> params) {
        Long start = System.currentTimeMillis();
        try {
            PreparedStatement ps = conn.prepareStatement(sql);
            for (BaseParam param : params) {
                switch (param.getType()) {
                    case INT:
                        ps.setInt(param.getIndex(), (Integer) param.getValue());
                        break;
                    case DATE:
                        ps.setDate(param.getIndex(), (Date) param.getValue());
                        break;
                    case STRING:
                        ps.setString(param.getIndex(), (String) param.getValue());
                        break;
                    case DATETIME:
                        ps.setTimestamp(param.getIndex(), (Timestamp) param.getValue());
                        break;
                    default:
                        throw new Exception("参数错误." + param.getType());
                }
            }
            ps.executeUpdate();
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        } finally {
            Long end = System.currentTimeMillis();
            System.out.println("开始时间:" + start + ",结束时间:" + end + ".总用时:" + (end - start));
        }
    }
}
